package pers.sloera.leetcode.editor.cn;

import java.util.Arrays;

/**
 * 矩阵中的局部最大值
 *
 * @author sloera
 * @date 2023-03-01 19:41:15
 */
class LargestLocalValuesInAMatrix {
    public static void main(String[] args) {
        Solution solution = new LargestLocalValuesInAMatrix().new Solution();
        int[][] ints = solution.largestLocal(new int[][]{{9, 9, 8, 1}, {5, 6, 2, 6}, {8, 2, 6, 4}, {6, 2, 2, 2}});
        ints = solution.largestLocal(new int[][] {{1,1,1,1,1},{1,1,1,1,1},{1,1,2,1,1},{1,1,1,1,1},{1,1,1,1,1}});
        System.out.println(Arrays.toString(ints));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int[][] largestLocal(int[][] grid) {
            int[][] ret = new int[grid.length - 2][grid.length - 2];
            for (int i = 1; i < grid.length - 1; i++) {
                for (int j = 1; j < grid[i].length - 1; j++) {
                    ret[i - 1][j - 1] = getMax(grid, i, j);
                }
            }
            return ret;
        }

        private int getMax(int[][] grid, int centerX, int centerY) {
            int ret = 0;
            for (int i = centerX - 1; i <= centerX + 1; i++) {
                for (int j = centerY - 1; j <= centerY + 1; j++) {
                    ret = Math.max(ret, grid[i][j]);
                }
            }
            return ret;
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)

}
